﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;



namespace View.LibrarianForms
{
    public partial class LibrarianEditForm : Form
    {
        private LibrarianEventArgs _properties;

        public LibrarianEditForm()
        {
            InitializeComponent();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Controls2Fields();
            OnSubmit();
            SafeClose();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            SafeClose();
        }

        private void SafeClose()
        {
            textBoxPassword.Text = "";
            _properties = null;
            Hide();
        }

        public void ShowDialog(LibrarianEventArgs librarianFields)
        {
            _properties = librarianFields;
            Fields2Controls();
            ShowDialog();
        }

        private void Controls2Fields()
        {
                _properties.Name = textBoxName.Text;
                _properties.Address = textBoxAddress.Text;
                _properties.Phone = textBoxPhone.Text;
                _properties.IsAdmin = checkBoxAdmin.Checked;
                _properties.Username = textBoxUsername.Text;
                _properties.Password = textBoxPassword.Text;
        }

        private void Fields2Controls()
        {
            textBoxName.Text = _properties.Name;
            textBoxAddress.Text = _properties.Address;
            textBoxPhone.Text = _properties.Phone;
            checkBoxAdmin.Checked = _properties.IsAdmin;
            textBoxUsername.Text = _properties.Username;
            textBoxPassword.Text = _properties.Password;
        }

        public event EventHandler<LibrarianEventArgs> Submit;

        private void OnSubmit()
        {
            if (Submit != null)
            {
                Submit(this, _properties);
            }
        }

        private void LibrarianEditForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

    }
}
